package com.qf.md.unit.controller;

import com.qf.common.PageParam;
import com.qf.common.PageVO;
import com.qf.common.R;
import com.qf.entity.md.unit.MdUnitMeasure;
import com.qf.md.unit.param.UnitSearchParam;
import com.qf.md.unit.service.MdUnitMeasureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/md/unit")
@Api(tags="计量单位管理")
@CrossOrigin
@Slf4j
public class MdUnitMeasureController {
    @Resource
    private MdUnitMeasureService mdUnitMeasureService;
    @GetMapping("/list")
    //R<T> ok(T data)  T ===> PageVO<MdUnitMeasure> 把这个值赋值给了data
    @ApiOperation("分页查询")
    public R<PageVO<MdUnitMeasure>> unitPage(UnitSearchParam unitSearchParam) {
        log.info("info");
        log.warn("warn");
        log.error("error");
        log.debug("debug");
        PageVO<MdUnitMeasure> mdUnitMeasurePageVO = mdUnitMeasureService.unitPage(unitSearchParam);
        System.out.println(mdUnitMeasurePageVO);
        return R.ok(mdUnitMeasurePageVO);
        //{
        //  code:2000,
        //   msg:"",
        //   data:{
        //     total:100,
        //     dataList:[{},{},{}]
        //   }
        // }
    }
    //'/md/unit/add',
    @PostMapping("/save")
    public R addMdUnitMeasure(@RequestBody MdUnitMeasure mdUnitMeasure){
        log.info("保存的数据为：{}",mdUnitMeasure);
        System.out.println(mdUnitMeasure);
        int i = mdUnitMeasureService.addMdUnitMeasure(mdUnitMeasure);
        if(i>0){
            return  R.ok(mdUnitMeasure);
        }
        return R.ok("添加失败");
    }
    @PostMapping("/delete")
    public R delById(Long id){
        System.out.println(id);
        mdUnitMeasureService.delMdUnitMeasure(id);
        return R.ok("成功");
    }
    @PostMapping("/update")
    public R updateMdUnitMeasure(@RequestBody MdUnitMeasure mdUnitMeasure){
        System.out.println(mdUnitMeasure);
        mdUnitMeasureService.updateMdUnitMeasure(mdUnitMeasure);
        return R.ok(mdUnitMeasure);
    }
    @PostMapping("/delBatch")
    public R delBatch(@RequestBody List<Long> ids){
        System.out.println(ids);
        mdUnitMeasureService.delBatch(ids);
//        mdUnitMeasureService.delMdUnitMeasure(id);
        return R.ok("成功");
    }
    @GetMapping("/primaryUnit")
    public R primaryUnit(){
        List<MdUnitMeasure> select = mdUnitMeasureService.select();

        return R.ok(select);
    }
}
